<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>AJAX 返回数据问题</title>
	</head>
	<body>
		<script type="text/javascript">
			
			//封装者
			// =====================
			
			function ajax (method,url,params,done) {
				method = method.toUpperCase()
				var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP')
				
				if (typeof params === 'object') {
					var tempArr = []
					for (var key in params) {
						var value = params[key]
						tempArr.push(key + '=' + value)
					}
					params = tempArr.join('&');
				}
				
				if(method === 'GET') {
					url += '?' + params;
				}
				xhr.open(method,url)
				data = null
				if(method === 'POST') {
					xhr.setRequestHeader('Cotent-Type','application/x-www-form-urlencoded')
					data = params;
				}
				xhr.send(data)
				
				xhr.onreadystatechange = function () {
					if (this.readyState !== 4) return;
					// 不应该在封装的函数中主观的处理响应结果
					// console.log(this.responseText)
					// 你说我太主观,那么你告诉我应该做什么
					done(this.responseText);
				}
				
			}
				
			
			// 调用者
			// ==========================
			
			var onDone = function (res) {
				console.log('hahahaha')
				console.log('hohohooo')
				console.log(res)
				console.log('做完了')
			}
			
			ajax('post','time.php',{key1:"valur1"},onDone)
			
		</script>
		
	</body>
</html>
